﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using SocketControl.Protocol.Http.Event;
using SocketControl.Protocol.Http.ResponseMessage;

namespace SocketControl.Protocol.Http
{
    public class GetGenernalData
    {
        protected MemoryStream readStream = null;
        protected bool readResponseRealy = false;
        protected bool readChunkedRealy = false;
        protected int httpResponseOffset = 0;
        protected int dataSize = 0;
        protected byte[] bodyData = null;

        protected ResponseMessage.IResponse response = new ResponseMessage.Response();

        protected EventHandler<Event.HttpEventArgs> GetHttpResponseRealy;

        public event EventHandler<Event.HttpEventArgs> GetBodyCompleted;

        object lockObj = new object();

        private string responseData = string.Empty;
        public string GetResponseData
        {
            get { return responseData; }
        }

        public int GetChunkedSize
        {
            get { return dataSize; }
        }



        #region method
        public void getData(byte[] param)
        {
            if (!readResponseRealy)
            {
                GetHttpResponse(param);
            }
        }

        protected void GetHttpResponse(byte[] param)
        {
            if (readStream == null)
            {
                readStream = new MemoryStream();
            }
            readStream.Write(param, 0, param.Length);
            #region getHttpResponse
            int offset = 0;
            for (offset = 0; offset < param.Length; offset++)
            {
                ++httpResponseOffset;
                if (offset >= 3)
                {
                    if (param[offset - 3] == 13 && param[offset - 2] == 10 && param[offset - 1] == 13 && param[offset] == 10)
                    {
                        byte[] httpBuff = new byte[httpResponseOffset];
                        Array.Copy(readStream.GetBuffer(), 0, httpBuff, 0, httpResponseOffset);
                        responseData = Encoding.UTF8.GetString(httpBuff);
                        readResponseRealy = true;
                        break;
                    }
                }
            }
            if (readResponseRealy)
            {
                if (GetHttpResponseRealy != null)
                {
                    Event.HttpEventArgs args = new Event.HttpEventArgs();
                    byte[] buff = new byte[param.Length - offset];
                    Array.Copy(param, offset, buff, 0, param.Length - offset);
                    args.handle = this;
                    args.param = buff;
                    GetHttpResponseRealy(this, args);
                    buff = null;
                    readStream.Flush();
                }
            }
            #endregion
        }

        protected void GetBody(byte[] param)
        {
            if (param.Length > 0)
            {
                if (dataSize - param.Length >= 0)
                {
                    readStream.Write(param, 0, param.Length);
                }
            }
        }
        #endregion
    }
}
